var {
    get,
    post
} = require('./../../comm/localHttp');

var cssd = require('../../comm/cssdHelper');
Page({
    data: {
        examId: '',
        totalQuestions: 0,
        answeredQuestions: [],
        examData: {},
        process: 0,
        processInfo: '',
    },

    onLoad: function (e) {
        console.log(e);
        var examId = e.examId;

        const userInfo = wx.getStorageSync('userInfo');
        this.setData({
            examId: examId
        });
        this.loadExamData(examId, userInfo.userId);
    },

    setProcessInfo() {
        if (this.data.examData == null || this.data.examData.listHistoryQuestion == null || this.data.examData.listHistoryQuestion.length == 0) {
            this.setData({
                process: 0,
                processInfo: '无题目'
            });
        } else {
            this.setData({
                process: (this.data.currentQuestionIndex + 1) * 100 / this.data.examData.listHistoryQuestion.length,
                processInfo: (this.data.currentQuestionIndex + 1) + '/' + this.data.examData.listHistoryQuestion.length,
            });
        }
    },
    loadExamData: async function (examId, userId) {
        var examData = {};
        var currentQt = {};
        var currentQuestionIndex = -1;
        const res = await get('/publish/exam_user', {
            examId: examId,
            userId: userId,
        });

        if (res.code == 0) {
            console.log('exam data:', res.data);
            examData = res.data; 
            for (let i = 0; i < examData.listExamUserAnswerVo.length; i++) {
                examData.listExamUserAnswerVo[i].index = i + 1;
                const qt = examData.listExamUserAnswerVo[i];
                qt.qtTypeStr = cssd.qtTypeAsStr(qt.qtType);
                const listItemInfo = qt.listItemInfo;
                const listUserAnswer = qt.listUserAnswer;
                for (let j = 0; j < listItemInfo.length; j++) {
                    for (let k = 0; k < listUserAnswer.length; k++) {
                        if (listItemInfo[j].qbItemId == listUserAnswer[k].answerId) {
                            listItemInfo[j].userChecked = true;
                        }
                    }
                }

            }
        }
        this.setData({
            examData: examData,
        });
        // this.setProcessInfo();
        // console.log(examData);
    },

    onOptionChange: function (e) {

        console.log('radio发生change事件，携带value值为：', e)
        const currentQt = this.data.currentQt;
        for (let i = 0, len = currentQt.listItemInfo.length; i < len; ++i) {
            currentQt.listItemInfo[i].checked = (i == e.currentTarget.dataset.index);
            console.log('checked', currentQt.listItemInfo[i].checked)
        }
        this.setData({
            currentQt
        })
    },

    onOptionMutilChange: function (e) {
        console.log('radio发生change事件，携带value值为：', e)
        const currentQt = this.data.currentQt;
        const index = e.currentTarget.dataset.index;
        currentQt.listItemInfo[index].checked = !currentQt.listItemInfo[index].checked;
        this.setData({
            currentQt
        })
    },

    btn_click_pre: function () {
        if (this.data.currentQuestionIndex > 0) {
            if (this.data.examData != null && this.data.examData.listHistoryQuestion != null &&
                this.data.currentQuestionIndex - 1 < this.data.examData.listHistoryQuestion.length) {
                this.setData({
                    currentQt: this.data.examData.listHistoryQuestion[this.data.currentQuestionIndex - 1],
                    currentQuestionIndex: this.data.currentQuestionIndex - 1,
                });
            }
        }
        this.setProcessInfo();
    },

    btn_click_next: function () {
        if (this.data.currentQuestionIndex < this.data.examData.listHistoryQuestion.length - 1) {
            this.setData({
                currentQt: this.data.examData.listHistoryQuestion[this.data.currentQuestionIndex + 1],
                currentQuestionIndex: this.data.currentQuestionIndex + 1,
            });
        }
        this.setProcessInfo();
    },
    btn_click_submit: async function () {
        const userInfo = wx.getStorageSync('userInfo');

        const examUserInfo = {
            answerStatus: 1,
            userId: userInfo.userId,
            examId: this.data.examId,
            OfficeCode: userInfo.officeCode,
            listUserAnswer: [],
        };
        let score = 0;
        for (let i = 0, len = this.data.examData.listHistoryQuestion.length; i < len; i++) {
            console.log(i);
            var qt = this.data.examData.listHistoryQuestion[i];
            var hasSelect = false;
            for (let j = 0, lenJ = qt.listItemInfo.length; j < lenJ; j++) {
                console.log(j);
                const option = qt.listItemInfo[j];
                if (option.checked) {
                    hasSelect = true;
                    examUserInfo.listUserAnswer.push({
                        userId: userInfo.userId,
                        examId: this.data.examId,
                        hqbId: option.hqbId,
                        answerId: option.qbItemId,
                        score: option.score
                    });
                    score += option.score;
                }
            }
            examUserInfo.score = score;
            if (hasSelect == false) {
                wx.showModal({
                    title: '还有内容未完成',
                    content: '第 ' + (i + 1) + ' 未答题!'
                })
                return;
            }
        }
        console.log('examUserInfo', examUserInfo);
        wx.showModal({
            title: '提示',
            content: '点击提交问卷',
            success: async (res) => {
                if (res.confirm) {
                    const res = await post('/publish/answer', examUserInfo);
                    if (res.code == 0) {
                        wx.showModal({
                            title: '提示!',
                            content: '已提交'
                        });
                        wx.navigateTo({
                            url: '/pages/index/index'
                        });
                    } else {
                        wx.showModal({
                            title: '错误',
                            content: 'code:' + res.code + ';msg:' + res.msg
                        });
                    }
                }
            }
        })
    }
});